import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import net.miginfocom.swing.*;
/*
 * Created by JFormDesigner on Tue Dec 17 00:53:26 CST 2024
 */



/**
 * @author jjchen
 */
public class Passwordchange_user extends JFrame {
    public Passwordchange_user() {
        initComponents();
    }

    private void button1MouseClicked(MouseEvent e) {
        this.dispose();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        user_Home u = new user_Home();
        u.setVisible(true);    }

    private void button1MouseClicked2(MouseEvent e) {
        String currentPassword = passwordField1.getText(); // 当前密码
        String newPassword = passwordField2.getText();     // 新密码
        String confirmNewPassword = passwordField3.getText(); // 确认新密码

        String url = "jdbc:mysql://localhost:3306/javaproject";
        String username = "root";
        String rootPassword = "root";

        String sqlSelect = "SELECT password FROM user_information WHERE user_ID = ?";
        String sqlUpdate = "UPDATE user_information SET password = ? WHERE user_ID = ?";

        Connection conn = null;
        PreparedStatement pstmtSelect = null;
        PreparedStatement pstmtUpdate = null;
        ResultSet rs = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url, username, rootPassword);

            pstmtSelect = conn.prepareStatement(sqlSelect);
            pstmtSelect.setString(1, Lgoin_user.userid);
            rs = pstmtSelect.executeQuery();

            if (rs.next()) {
                String storedPassword = rs.getString("password");

                if (storedPassword.equals(currentPassword)) {
                    if (newPassword.equals(confirmNewPassword)) {
                        pstmtUpdate = conn.prepareStatement(sqlUpdate);
                        pstmtUpdate.setString(1, newPassword);
                        pstmtUpdate.setString(2, Lgoin_user.userid);
                        JOptionPane.showMessageDialog(this, "密码更改成功！");

                        int rowsAffected = pstmtUpdate.executeUpdate();
                        if (rowsAffected > 0) {
                            // 提交事务
                            conn.commit();
                        } else {
                            // 回滚事务
                            conn.rollback();
                            JOptionPane.showMessageDialog(this, "密码更改失败。");
                        }
                    } else {
                        JOptionPane.showMessageDialog(this, "新密码和确认密码不一致。");
                    }
                } else {
                    JOptionPane.showMessageDialog(this, "当前密码输入有误。");
                }
            } else {
                JOptionPane.showMessageDialog(this, "没有找到用户ID为 " + Lgoin_user.userid + " 的用户。");
            }

        } catch (SQLException se) {
            try {
                // 如果发生异常，回滚事务
                if (conn != null) conn.rollback();
            } catch (SQLException rollbackEx) {
                rollbackEx.printStackTrace();
            }
        } catch (ClassNotFoundException e1) {
            // 处理 Class.forName 错误
            e1.printStackTrace();
            JOptionPane.showMessageDialog(this, "JDBC 驱动加载失败！");
        } finally {
            // 关闭资源
            try {
                if (rs != null) rs.close();
                if (pstmtSelect != null) pstmtSelect.close();
                if (pstmtUpdate != null) pstmtUpdate.close();
                if (conn != null) conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
    }

    private void button2MouseClicked(MouseEvent e) {
        this.dispose();
        user_Home u = new user_Home();
        u.setVisible(true);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        button2 = new JButton();
        label8 = new JLabel();
        label3 = new JLabel();
        passwordField1 = new JPasswordField();
        label4 = new JLabel();
        passwordField2 = new JPasswordField();
        label6 = new JLabel();
        passwordField3 = new JPasswordField();
        label5 = new JLabel();
        button1 = new JButton();

        //======== this ========
        var contentPane = getContentPane();
        contentPane.setLayout(new MigLayout(
            "hidemode 3",
            // columns
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]",
            // rows
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]"));

        //---- button2 ----
        button2.setText("\u8fd4\u56de");
        button2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button2MouseClicked(e);
            }
        });
        contentPane.add(button2, "cell 2 1");

        //---- label8 ----
        label8.setText("\u4fee\u6539\u5bc6\u7801");
        label8.setFont(label8.getFont().deriveFont(label8.getFont().getSize() + 5f));
        contentPane.add(label8, "cell 7 1 1 2");

        //---- label3 ----
        label3.setText("\u539f\u59cb\u5bc6\u7801\uff1a");
        contentPane.add(label3, "cell 3 7");
        contentPane.add(passwordField1, "cell 5 7 7 1");

        //---- label4 ----
        label4.setText("\u65b0\u5bc6\u7801\uff1a");
        contentPane.add(label4, "cell 3 10");
        contentPane.add(passwordField2, "cell 5 10 7 1");

        //---- label6 ----
        label6.setText("\u786e\u8ba4\u5bc6\u7801\uff1a");
        contentPane.add(label6, "cell 3 14");
        contentPane.add(passwordField3, "cell 5 14 7 1");
        contentPane.add(label5, "cell 1 18 13 1");

        //---- button1 ----
        button1.setText("\u786e\u5b9a\u4fee\u6539");
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button1MouseClicked(e);
                button1MouseClicked2(e);
            }
        });
        contentPane.add(button1, "cell 7 19");
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    public static void main(String[] args) {
        Passwordchange_user window = new Passwordchange_user();
        window.setVisible(true);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JButton button2;
    private JLabel label8;
    private JLabel label3;
    private JPasswordField passwordField1;
    private JLabel label4;
    private JPasswordField passwordField2;
    private JLabel label6;
    private JPasswordField passwordField3;
    private JLabel label5;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
